package basic;

import misc.Util;
	/* this class provides basic element of key creation
	 * each key is generated with at least one sequence
	 * 
	 * @author Dusan Guduric
	 * */
public class Sequence {
	/* textual representation of one sequence. can contain characters and numbers*/
	private String sequence = null;
	/* sum that is used for checking validity of key generated
	 * based on generation method checksum can vary*/
	private int checkSum = 0;
	/* sequence constructor that takes two params
	 * @param sequence is textual representation, can contain text and numbers
	 * @param sum a checksum calculated based on sequence lenght and elements 
	 * that contain. varies for different generator methods used
	 * */
	public Sequence(String sequence, int sum) {
		this.sequence = sequence;
		this.checkSum = sum;
	}
	/* default sequence constructor without params*/
	public Sequence (){
		this.sequence = "";
		this.checkSum = 0;
	}
	
	public Sequence subSequence(int beginIndex){
		this.sequence = sequence.substring(beginIndex);
		this.checkSum = getSequenceSum(sequence);
		return this;
	}
	
	public Sequence subSequence(int beginIndex, int endIndex){
		this.sequence = sequence.substring(beginIndex, endIndex);
		this.checkSum = getSequenceSum(sequence);
		return this;
	}
	
	public int getSequenceSum(String sequence){
		int sum = 0;
		for (int i = 0; i < sequence.length(); i++){
			char c = sequence.charAt(i);
			if (Character.isLetter(c)){
				sum = sum + Util.convertCharToInt(c);
			} else {
				sum = sum + Integer.parseInt(Character.toString(c));
			}
		}
		return sum;
	}
	
	public String getSequence() {
		return sequence;
	}
	public void setSequence(String sequence) {
		this.sequence = sequence;
	}
	public int getSum() {
		return checkSum;
	}
	public void setSum(int sum) {
		this.checkSum = sum;
	}
}
